import http from "../service/http.js"
import Lodash from "lodash"
export const addressModule = {
	state: {
		regionLevel: 3, //地区层级数
		regionList: [
			// 地区级联列表

			// {
			// 	"id": 110000,
			// 	"name": "北京",
			// 	"pid": 100000,
			// 	"sname": "北京",
			// 	"level": 1,
			// 	"regionCode": "",
			// 	"yzCode": "",
			// 	"mergeName": "中国,北京",
			// 	"longitude": 116.405,
			// 	"latitude": 39.905,
			// 	"pinyin": "Beijing",
			// 	children:[
			// 		{
			// 		      "id": 110100,
			// 		      "name": "北京市",
			// 		      "pid": 110000,
			// 		      "sname": "北京",
			// 		      "level": 2,
			// 		      "regionCode": "010",
			// 		      "yzCode": "100000",
			// 		      "mergeName": "中国,北京,北京市",
			// 		      "longitude": 116.405,
			// 		      "latitude": 39.905,
			// 		      "pinyin": "Beijing"
			// 		    }
			// 	],

			// },
		],
		areaPickerList: [],
		actAddress: {
			// addresAlias: null
			// addresType: 0
			// address: "榄树排东街12巷3号"
			// city: "广州市"
			// county: "黄埔区"
			// createBy: ""
			// createTime: "2021-01-11 03:40:27"
			// fixIphone: null
			// id: 20
			// iphone: "18202074132"
			// isDefault: 1
			// params: Object
			// province: "广东省"
			// remark: null
			// searchValue: null
			// takeName: "饶东俊"
			// updateBy: ""
			// updateTime: "2021-01-11 03:40:59"
			// userId: 3
		}, //当期选中地址
		addressList: [
			// 地址列表
		],
		// focuseTakeAddressList:[
		// 	// 集中收货点地址列表

		// ],
		editAddressFrom: {

		}
	},
	mutations: {
		setRegionList(state, regionList) {
			state.regionList = regionList;
		},
		setAreaPickerList(state, areaPickerList) {
			state.areaPickerList = areaPickerList;
		},
		setAddressList(state, addressList) {

			state.addressList = addressList;
		},
		setAddressFrom(state, editAddressFrom) {

			state.editAddressFrom = editAddressFrom;
		},
		setActAddress(state, actAddress) {

			state.actAddress = actAddress;
		},


	},
	getters: {

	},
	actions: {

		getPickerListfromRegionList: async function({
			commit,
			state
		}) {
			
				// 解析地区数据生成picker数据
				let regionList = state.regionList
				let work = data => {
					if (data && data.length > 0)
						return data.map(item => {
							let result = {
								id: item.id,
								label: item.name,
								value: item.name
							};
							if (item.childrenList && item.childrenList.length > 0) {
								result.children = work(item.childrenList);
							}
							return result;
						});
					else return [];
				};
				return work(regionList);
		
		},

		getAddressList: async function({
			commit,
			state
		}) {
			// 获取用户地址列表
			return await new Promise((resolve, reject) => {

				http.get('/address/ByUserAddress').then(res => {

					console.log("获取用户地址列表成功", res)
					let data = res.data.data
					commit("setAddressList", data)
					let actAddress = Lodash.find(data, {
						isDefault: 1
					})
					actAddress = actAddress && actAddress != -1 ? actAddress : {}
					commit("setActAddress", actAddress)
					commit("setTakeAddress", actAddress)
					resolve(res)

				}).catch(err => {
					console.log("获取用户地址列表失败", err)
					reject(err)
				})
			})
		},
		getFocuseTakeAddressList: async function({
				commit,
				state
			}, params
			// {
			// city: "广州市"
			// county: "黄埔区"
			// province: "广东省"
			// }
		) {
			// 获取集中收货点列表
			return await new Promise((resolve, reject) => {

				http.get('/address/focusingList', params).then(res => {

					console.log("获取查询集中收货点列表成功", res)
					let data = res.data.data


					resolve(res)

				}).catch(err => {
					console.log("获取用户地址列表失败", err)
					reject(err)
				})
			})
		},
		getRegionList: async function({
				commit,
				dispatch,
				state
			},

		) {
			// 获取地区数据列表
			return await new Promise((resolve, reject) => {

				http.get('/region/all').then(res => {
					console.log("获取地区级联列表成功", res)
					let data = res.data.data

					commit("setRegionList", data)
					dispatch("getPickerListfromRegionList").then(result=>{
						commit("setAreaPickerList", result)
					})

					resolve(res)

				}).catch(err => {
					console.log("获取地区级联列表失败", err)
					reject(err)
				})
			})
		},
		addAddressInfo: async function({
			commit,
			dispatch,
			state
			// {
			// address
			// city
			// county
			//       province
			//       takeName
			//       iphone
			// }
		}, params) {
			// console.log("params", params)

			params.isDefault = params.isDefault ? 1 : 0
			commit("setAddressFrom", params)
			return await new Promise((resolve, reject) => {

				http.post('/address/add', state.editAddressFrom).then(res => {

					console.log("添加地址信息成功", res)
					let data = res.data.data
					dispatch("getAddressList")
					resolve(data)

				}).catch(err => {
					console.log("添加地址信息失败", err)
					reject(err)
				})
			})
		},
		editAddressInfo: async function({
			commit,
			dispatch,
			state
			// {
			// address
			// city
			// county
			//       province
			//       takeName
			//       iphone
			// }
		}, params) {
			// console.log("params", params)
			params.isDefault = params.isDefault ? 1 : 0
			commit("setAddressFrom", params)
			return await new Promise((resolve, reject) => {

				http.post('/address/edit', state.editAddressFrom).then(res => {

					console.log("编辑地址信息成功", res)
					let data = res.data.data
					dispatch("getAddressList")
					resolve(data)

				}).catch(err => {
					console.log("编辑地址信息失败", err)
					reject(err)
				})
			})
		},
		removeAddress: async function({
			commit,
			dispatch,
			state
			// {
			// address
			// city
			// county
			//       province
			//       takeName
			//       iphone
			// }
		}, params) {
			// console.log("params", params)
			return await new Promise((resolve, reject) => {

				http.post(`/address/remove/${params.id}`).then(res => {

					console.log("删除地址信息成功", res)
					let data = res.data.data
					dispatch("getAddressList")
					resolve(data)

				}).catch(err => {
					console.log("删除地址信息失败", err)
					reject(err)
				})
			})
		},
	}
}
